package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.constants.RedisConstants;
import cn.gyx.hrm.domain.CourseType;
import cn.gyx.hrm.mapper.CourseTypeMapper;
import cn.gyx.hrm.service.ICourseTypeService;
import cn.gyx.hrm.doc.vo.CrumbsVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author gyx
 * @since 2021-11-26
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    @CacheEvict(cacheNames = RedisConstants.KEY_BEFORE_COURSE_TYPE,key = RedisConstants.KEY_COURSE_TYPE_TREE)
    public boolean insert(CourseType entity) {
        boolean suc = super.insert(entity);
        return suc;
    }

    @Override
    @CacheEvict(cacheNames = RedisConstants.KEY_BEFORE_COURSE_TYPE,key = RedisConstants.KEY_COURSE_TYPE_TREE)
    public boolean updateById(CourseType entity) {
        boolean suc = super.updateById(entity);
        return suc;
    }

    @Override
    @CacheEvict(cacheNames = RedisConstants.KEY_BEFORE_COURSE_TYPE,key = RedisConstants.KEY_COURSE_TYPE_TREE)
    public boolean deleteById(Serializable id) {
        boolean suc = super.deleteById(id);
//        if (suc) {
//            redisTemplate.delete(RedisConstants.KEY_COURSE_TYPE_TREE);
//        }
        return suc;
    }

    public List<CourseType> selectTreesData() {

//        return baseMapper.selectTree(0L);
        //1.查询所有的分类
        List<CourseType> selectList = super.selectList(null);
        //2.找出所有第一层 if 判断是否第一层 else 其他层
        List<CourseType> list = new ArrayList<>();

        selectList.forEach(courseType -> {
            if (courseType.getPid() == null || courseType.getPid().longValue() == 0) {
                list.add(courseType);
            } else {
                //3.查询其他非底层
                for (CourseType everCourseType : selectList) {
                    if (courseType.getPid().longValue() == everCourseType.getId().longValue()) {
                        everCourseType.getChildren().add(courseType);
                        break;
                    }
                }
            }
        });
        return list;
    }

    @Override
    @Cacheable(cacheNames = RedisConstants.KEY_BEFORE_COURSE_TYPE, key = RedisConstants.KEY_COURSE_TYPE_TREE)
    public List<CourseType> selectTreeData() {
        return selectTreesData();
    }

    /*
     * 面包屑
     */
    @Override
    public List<CrumbsVo> crumbs(Long id) {
        //根据id查询课程分类
        CourseType courseType = super.selectById(id);
        //查询path 切割
        String[] path = courseType.getPath().split("\\.");
        //批量查询课程分类
        List<CourseType> courseTypeList = super.selectBatchIds(Arrays.asList(path));
        List<CrumbsVo> result = new ArrayList<>(path.length);
        //循环课程分类，一个封装成一个CrumbsVo
        courseTypeList.forEach(type->{
            CrumbsVo vo = new CrumbsVo();
            vo.setOwnerCourseType(type);
            //为每个分类查询统计分类，添加到CrumbsVo
            EntityWrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",type.getPid() );
            List<CourseType> other = super.selectList(wrapper);
            //将所有CrumbsVo 封装List返回
            vo.setOtherCourseTypes(other);
            result.add(vo);
        });
        return result;
    }

    public List<CourseType> selectTreeData1() {
        //1.先查缓存，是否有数据
        List<CourseType>  courseTypeList =
                (List<CourseType>) redisTemplate.opsForValue().get(RedisConstants.KEY_COURSE_TYPE_TREE);
        //2.如果有直接返回
        if (courseTypeList != null){
            return courseTypeList;
        }
        //3.没有则查数据库
        //4.把数据库中的数据转成treeData
        courseTypeList = selectTreesData();

        //5.treeData放到缓存
        redisTemplate.opsForValue().set(RedisConstants.KEY_COURSE_TYPE_TREE,courseTypeList,12, TimeUnit.DAYS);
        //6.返回数据

        return courseTypeList;
    }
}
